<template>
  <div class="login-bg">
    <div class="login-container">
      <el-card class="login-card">
        <div class="login-header">
          <div>
            <div class="login-title">WMS 仓储管理系统</div>
            <div class="login-subtitle">欢迎登录</div>
          </div>
        </div>
        <el-form :model="form" label-width="auto" class="login-form">
          <el-form-item label="用户名">
            <el-input v-model="form.Name" placeholder="请输入用户名" clearable />
          </el-form-item>
          <el-form-item label="用户密码">
            <el-input v-model="form.PassWord" show-password placeholder="请输入密码" clearable />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="login-btn" @click="Login()">登录</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { useRouter } from 'vue-router'
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { reactive } from 'vue'
import { useCounterStore } from '@/stores/counter'

import { http } from '@/axios/axiosHelpers'
import { systeamread } from '@/systeamaxios/sysaxiosHelpers'
const store = useCounterStore()
const router = useRouter()
const form = reactive({
  Name: "",
  PassWord: ""
})
//登录
const Login = () => {
  http.get("/managementsysteamreadapis/api/UserValues/Login", {
  
  //systeamread.get("/api/UserValues/Login", {
    params: {
      User_Name: form.Name,
      User_PassWord: form.PassWord
    }
  }).then(res => {
    if (res.data.code == 200) {
      ElMessage.success(res.data.message)
      // 正确获取 token 和用户名
      window.localStorage.setItem("token", res.data.data.token);
      axios.defaults.headers.common["Authorization"] = "Bearer " + res.data.data.token;
      localStorage.setItem("Rid", res.data.data.rid);
      localStorage.setItem("userName",res.data.data.user_Name);
      store.Account.token = res.data.data.token
      store.Account.name = res.data.data.user_Name
      store.Account.rid = res.data.data.rid || 0 // 将rid保存到store中
      router.push('/MainView?rid='+localStorage.getItem("Rid"))
      console.log(store.Account.rid)
    }
    else {
      ElMessage.success(res.data.message)
    }
  })
}
</script>

<style scoped>
.login-bg {
  min-height: 100vh;
  width: 100vw;
  background: linear-gradient(135deg, #e0e7ff 0%, #f0f2f5 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100vw;
  height: 100vh;
}
.login-card {
  width: 380px;
  padding: 40px 32px 32px 32px;
  border-radius: 18px;
  box-shadow: 0 8px 32px 0 rgba(31, 56, 88, 0.15);
  background: #fff;
}
.login-header {
  display: flex;
  align-items: center;
  margin-bottom: 32px;
}
.login-logo {
  width: 48px;
  height: 48px;
  margin-right: 18px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}
.login-title {
  font-size: 22px;
  font-weight: bold;
  color: #22223b;
}
.login-subtitle {
  font-size: 14px;
  color: #6c6f7e;
  margin-top: 4px;
}
.login-form {
  margin-top: 0;
}
.el-form-item {
  margin-bottom: 22px;
}
.login-btn {
  width: 100%;
  font-size: 16px;
  letter-spacing: 2px;
  border-radius: 6px;
  height: 40px;
}
</style>